package com.echo.code.thread;

import com.echo.code.thread.queue.BlockingQueue;
import com.echo.code.thread.queue.impl.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

/**
 * TestCase
 */
public class QueueTestCase {

  public static final BlockingQueue<Event> eventQueue = new LinkedBlockingQueue<>();

  @Test
  public static void QueueTestCase() {
    new Thread(() -> {
      for (int i = 0; i < 1000; i++) {
        eventQueue.offer(new Event(Integer.toString(i)));
      }
    }, "Producer").start();

    new Thread(() -> {
      for (int i = 0; i < 1000; i++) {
        Event event = eventQueue.take();
        System.out.println(event.getEventName());
        try {
          TimeUnit.MILLISECONDS.sleep(2);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }, "Consumer").start();
  }


  public static void main(String[] args) {
    QueueTestCase();
  }

  public static class Event {

    private String eventName;

    public Event(String eventName) {
      this.eventName = eventName;
    }

    public String getEventName() {
      return eventName;
    }
  }


}
